package com.leetcode.LeetCode精选TOP面试题;

/**
 * @author 覃国强
 * @date 2022-05-25 11:44
 */
public class 题库_0050_Pow_x_n {

  static
  class Solution {
    public double myPow(double x, int n) {
      if (x == 0) {
        return 0.0;
      }
      if (n == 0) {
        return 1.0;
      }
      boolean isNegative = n < 0;
      boolean isMinValue = n == Integer.MIN_VALUE;
      n = isNegative ? (isMinValue ? Integer.MAX_VALUE : -n) : n;
      double temp = x;
      double answer = 1.0;
      while (n > 0) {
        if ((n & 1) == 1) {
          answer *= temp;
        }
        temp *= temp;
        n >>= 1;
      }
      return isNegative ? (isMinValue ? 1.0 / (answer * x) : 1.0 / answer) : answer;
    }
  }

}
